angular.module("crawlerApp").controller("quartzTaskCtrl", function ($scope, $http, $state, $stateParams, menuService, $rootScope) {
    $scope.quartzConfig = {
        ruleId: "",
        cron: ""
    };
    $scope.isRun=false;
    $scope.hasQuartz = false;
    $scope.config = $stateParams.config;
    addQuartz();

    function addQuartz() {
        $scope.quartzConfig.ruleId = $scope.config.id;
        $http.get("crawler/quartz/get/" + $scope.quartzConfig.ruleId).then(function (response) {
            if (response.data.length !== 0) {
                $scope.hasQuartz = true;
                $scope.quartzConfig.cron = response.data[0].cron;
            }
        })
    };


    $scope.addQuartzSure = function (config) {

        $http.put("crawler/quartz/add", config).then(function (response) {
            if (response.status == "200") {
                alert("设置成功!");
            }
        })

    };
    $scope.cancelQuartz = function (config) {
        $scope.dataToCancel = angular.copy(config);
    };

    $scope.cancelQuartzSure = function () {
        $http.post("crawler/quartz/cancel", $scope.dataToCancel).then(function (response) {
            if (response.status == "200") {
                alert("取消成功!");
            }
        })
    };

    $scope.modifyQuartzSure = function (config) {
        $http.post("crawler/quartz/modify", config).then(function (response) {
            if (response.status == "200") {
                alert("修改成功!");
            }
        })
    };

    $scope.cancel = function () {
        addQuartz();
    };

    $scope.start = function () {
        $scope.isRun=true;
        $http.get("crawler/part/page/" + $scope.config.id).then(function (response) {
            if (response.status == '200') {
                $scope.isRun=false;

               // alert("页面下载完成");
            }
            isEnd();
        });
    };
    isEnd();
    function isEnd(){
        $http.get("crawler/task/getTaskById/" + $scope.config.id).then(function (response) {
            if(response.data.length===0){
                $scope.isEnd=true;
            }else{
                if(response.data[0].status==='RUNNING'){
                    $scope.isEnd=false;
                }else {
                    $scope.isEnd=true;
                }
            }
        });
    }


});